﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BbsbWebService.Classes;
using System.IO;
using System.Xml.Linq;
using BbsbWebService.Enums;

namespace BbsbWebService.Repositories
{
    public class ServerItemRepository : ItemRepositoryBase
    {
        public override void Initialise()
        {
            if (File.Exists(AppSettings.SettingsFile.FullName))
            {
                XDocument doc = XDocument.Load(AppSettings.SettingsFile.FullName);

                var items = doc.Descendants("Items");
                foreach (var bob in items.Elements("Item"))
                {
                    ControllableItem item = new ControllableItem();
                    item.Id = new Guid(bob.Element("Id").Value);
                    item.Name = bob.Element("Name").Value;
                    item.Command = bob.Element("Command").Value;
                    foreach (var xmlGroup in bob.Descendants("Group"))
                    {
                        Guid id = new Guid(xmlGroup.Value);
                        if (GroupRepository.Instance.HasGroup(id))
                        {
                            Group group = GroupRepository.Instance.GetGroup(id);
                            item.Groups.Add(group);
                        }
                    }
                    Items.Add(item);
                }
            }
        }

        public override void RemoveItem(ControllableItem item)
        {
            Items.Remove(item);
            AppSettings.Save();
        }

        public override void SaveItem(ControllableItem item)
        {
            if (!string.IsNullOrEmpty(item.Name))
            {
                if (HasItem(item.Id))
                {
                    // update
                    for (int i = 0; i < Items.Count; i++)
                    {
                        if (Items[i].Id == item.Id)
                        {
                            Items[i] = item;
                            break;
                        }
                    }
                }
                else
                {
                    // new item
                    Items.Add(item);
                }
                AppSettings.Save();
            }
        }

        protected override void SendPowerBase(ControllableItem item, PowerOption option)
        {
            Controllers.SendCommand(item, option);
        }

        public override PowerState Toggle(Guid id)
        {
            PowerState retState = PowerState.Unknown;

            if (HasItem(id))
            {
                var item = GetItem(id);
                PowerState currState = item.State;
                if (currState == PowerState.On)
                {
                    SendPowerBase(item, PowerOption.Off);
                }
                else
                {
                    SendPowerBase(item, PowerOption.On);
                }
                retState = item.State;
            }

            return retState;
        }
    }
}
